<?php
trait Singleton {
  private static $instances = [];

  protected function __construct()
  {
  }

  /**
   * Cloning and unserialization are not permitted for singletons.
   */
  protected function __clone()
  {
  }

  public function __wakeup()
  {
    throw new \Exception("Cannot unserialize singleton");
  }

  /**
   * The method you use to get the Singleton's instance.
   */
  public static function getInstance()
  {
    $subclass = static::class;
    if (!isset(self::$instances[$subclass])) {
      self::$instances[$subclass] = new static();
    }
    return self::$instances[$subclass];
  }
}

/*
class Singleton
{
  private static $instances = [];

  protected function __construct()
  {
  }

  protected function __clone()
  {
  }

  public function __wakeup()
  {
    throw new \Exception("Cannot unserialize singleton");
  }

  public static function getInstance()
  {
    $subclass = static::class;
    if (!isset(self::$instances[$subclass])) {
      self::$instances[$subclass] = new static();
    }
    return self::$instances[$subclass];
  }
}
*/
